<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
             label-width="68px">
      <el-form-item label="盘点编号" prop="checkCode">
        <el-input v-model="queryParams.checkCode" placeholder="请输入盘点编号" clearable
                  @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="盘点结果" prop="checkResult">
        <el-select v-model="queryParams.checkResult" placeholder="请选择盘点结果" clearable>
          <el-option v-for="dict in dict.type.py_stocktaking_result" :key="dict.value" :label="dict.label"
                     :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="盘点类型" prop="checkType">
        <el-select v-model="queryParams.checkType" placeholder="请选择盘点类型" clearable>
          <el-option v-for="dict in dict.type.py_stocktaking_type" :key="dict.value" :label="dict.label"
                     :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
                   v-hasPermi="['check:check:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
                   v-hasPermi="['check:check:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-user" size="mini" @click="sh" :disabled="single">
          审核
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple"
                   @click="handleDelete" v-hasPermi="['check:check:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
                   v-hasPermi="['check:check:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="checkList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" prop="checkId" fixed width="50" />
      <el-table-column label="盘点编号" align="center" prop="checkCode" fixed width="160" />
      <el-table-column label="盘点结果" align="center" prop="checkResult" fixed width="120">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.py_stocktaking_result" :value="scope.row.checkResult" />
        </template>
      </el-table-column>
      <el-table-column label="仓库名称" align="center" prop="warehouseName" width="120" />
      <el-table-column label="盘点类型" align="center" prop="checkType" width="120">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.py_stocktaking_type" :value="scope.row.checkType" />
        </template>
      </el-table-column>
      <el-table-column label="盘点开始时间" align="center" prop="checkStarttime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.checkStarttime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="盘点结束时间" align="center" prop="checkEndtime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.checkEndtime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="出库状态" align="center" width="120">
        <template slot-scope="scope">
          <span v-if="scope.row.exWarehousing == null">-</span>
          <span v-else>
						<dict-tag :options="dict.type.od_inventory_transfer" :value="scope.row.exWarehousing" />
					</span>
        </template>
      </el-table-column>
      <el-table-column label="入库状态" align="center" width="120">
        <template slot-scope="scope">
          <span v-if="scope.row.warehousing == null">-</span>
          <span v-else>
						<dict-tag :options="dict.type.od_inventory_transfer" :value="scope.row.warehousing" />
					</span>
        </template>
      </el-table-column>
      <el-table-column label="盘点货品" align="center" prop="whCheckDetails" width="250"
                       :show-overflow-tooltip="true">
        <template slot-scope="scope">
					<span v-for="(item,index) in scope.row.whCheckDetails" :key="index">
						<!-- 显示数组元素的name属性 -->
						{{ item.product.productName }}
						<span v-if="index < scope.row.whCheckDetails.length - 1">,</span>
					</span>
        </template>
      </el-table-column>
      <el-table-column label="盘点数量" align="center" prop="checkNumber" width="120" />
      <el-table-column label="盘盈数量" align="center" prop="yesInventoryNumber" width="120">
        <template slot-scope="scope">
					<span :style="{color: (scope.row.yesInventoryNumber > 0) ? 'green':'#606266'}">
						{{scope.row.yesInventoryNumber}}
					</span>
        </template>
      </el-table-column>
      <el-table-column label="盘亏数量" align="center" prop="noInventoryNumber" width="120">
        <template slot-scope="scope">
					<span :style="{color: (scope.row.noInventoryNumber < 0) ? 'red':'#606266'}">
						{{scope.row.noInventoryNumber}}
					</span>
        </template>
      </el-table-column>
      <el-table-column label="盘盈金额" align="center" prop="yesInventoryPrice" width="120">
        <template slot-scope="scope">
					<span :style="{color: (scope.row.yesInventoryPrice > 0) ? 'green':'#606266'}">
						{{scope.row.yesInventoryPrice}}
					</span>
        </template>
      </el-table-column>
      <el-table-column label="盘亏金额" align="center" prop="noInventoryPrice" width="120">
        <template slot-scope="scope">
					<span :style="{color: (scope.row.noInventoryPrice < 0) ? 'red':'#606266'}">
						{{scope.row.noInventoryPrice}}
					</span>
        </template>
      </el-table-column>
      <el-table-column label="经办人" align="center" prop="handledBy" width="120" />
      <el-table-column label="创建人" align="center" prop="createBy" width="120" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="120">
        <template slot-scope="scope">
          <div v-if="scope.row.checkResult == 2">
            <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                       v-hasPermi="['check:check:edit']">修改</el-button>
            <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                       v-hasPermi="['check:check:remove']">删除</el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>

    <!-- 审核弹出框 -->
    <el-dialog title="审核" :visible.sync="shopen" width="500px">
      <el-form :model="shform">
        <el-form-item label="审核结果" prop="result">
          <el-radio-group v-model="shform.result">
            <el-radio :label="2">通过</el-radio>
            <el-radio :label="1">驳回</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="审核意见" prop="comments">
          <el-input type="textarea" v-model="shform.comments" placeholder="请输入"></el-input>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
				<el-button type="primary" @click="submitReview">确定</el-button>
				<el-button @click="shresetForm">取消</el-button>
			</span>
    </el-dialog>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
                @pagination="getList" />
  </div>
</template>

<script>
import {
  listCheck,
  getCheck,
  delCheck,
  addCheck,
  updateCheck,
  updateStatus,
} from "@/api/check/check";

export default {
  name: "Check",
  dicts: ['py_stocktaking_type', 'py_stocktaking_result', 'od_inventory_transfer'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: false,
      // 总条数
      total: 0,
      // 盘点列表表格数据
      checkList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        checkCode: null,
        checkResult: null,
        warehouseName: null,
        checkType: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        checkCode: [{
          required: true,
          message: "盘点编号不能为空",
          trigger: "blur"
        }],
        warehouseName: [{
          required: true,
          message: "仓库名称不能为空",
          trigger: "change"
        }],
        checkStarttime: [{
          required: true,
          message: "开始时间不能为空",
          trigger: "blur"
        }],
        checkEndtime: [{
          required: true,
          message: "结束时间不能为空",
          trigger: "blur"
        }],
      },
      //审核弹出
      shopen: false,
      shform: {
        result: 2,
        comments: '',
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    submitReview() {
      const check = this.checkList.find(item => item.checkId == this.ids);

      console.log(check);

      const data = {
        checkStatus: this.shform.result, // 假设结果是 0 或 1
        checkCode: check.checkCode,
      };

      updateStatus(data)
        .then(response => {
          console.log('审核提交成功:', response);
          this.getList();
        }).catch(error => {
        console.error('Fetching suppliers failed:', error);
      });
      this.shopen = false; // 关闭弹出框
    },
    shresetForm() {
      // 重置表单
      this.shform.result = 2;
      this.shform.comments = '';
      this.shopen = false; // 关闭弹出框
    },
    // 审核
    sh() {
      this.shopen = true; // 打开弹出框
    },
    /** 查询盘点列表列表 */
    getList() {
      this.loading = true;
      listCheck(this.queryParams).then(response => {
        console.log(response)
        this.checkList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        checkId: null,
        checkCode: null,
        checkResult: null,
        warehouseName: null,
        checkType: null,
        checkStarttime: null,
        checkEndtime: null,
        handledBy: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        isDelete: null,
        checkRemark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.checkId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
      const isTrue = selection.map(item => item.checkStatus);
      console.log(selection)
      if (isTrue[0] == 2) {
        this.single = true;
      }
    },
    /** 新增按钮操作 */
    handleAdd() {
      // this.reset();
      // this.open = true;
      // this.title = "添加盘点列表";

      this.$router.push({
        path: '/wms/check/check/checkadd',
        query: {
          //checkId: '6'
        }
      }); // 使用绝对路径
    },
    /** 修改按钮操作 */
    handleUpdate(row) {

      this.$router.push({
        path: '/wms/check/check/checkadd',
        query: {
          checkId: row.checkId
        }
      }); // 使用绝对路径
      // this.reset();
      // const checkId = row.checkId || this.ids
      // getCheck(checkId).then(response => {
      // 	this.form = response.data;
      // 	this.open = true;
      // 	this.title = "修改盘点列表";
      // });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.checkId != null) {
            updateCheck(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCheck(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const checkIds = row.checkId || this.ids;
      this.$modal.confirm('是否确认删除盘点列表编号为"' + checkIds + '"的数据项？').then(function() {
        return delCheck(checkIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('check/check/export', {
        ...this.queryParams
      }, `check_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
